package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.RemainingTimes;
import com.tanhua.dubbo.api.RemainingTimesApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@Service
public class RemainingTimesApiImpl implements RemainingTimesApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Long count(Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId));
        RemainingTimes remainingTimes = mongoTemplate.findOne(query, RemainingTimes.class);
        Integer count = remainingTimes.getRemainingTimes();

        return Long.valueOf(count);
    }

    @Override
    public void modifyTimes(int intValue,Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        Update update = new Update();
        update.inc("remainingTimes",-1);
        mongoTemplate.updateFirst(query, update,RemainingTimes.class);

    }
}
